<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
    <title>New Database Factory</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="../css/easyui.css">
    <link type="text/css" rel="stylesheet" href="../css/icon.css">
   	<script language="javascript" type="text/javascript" src="../js/jquery-1.8.3.js"></script>
    <script language="javascript" type="text/javascript" src="../js/jquery.easyui.min.js"></script>
  </head>
  <body>
	<div class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'center',border:false" style="overflow:auto;padding-left:100px;padding-top:30px;">
			<form id="form" method="post">
				<input type="hidden" name="key" value="${itemModel.key}">
				<fieldset style="border:1px">
				    <legend><div class="pagetitle">Factory Name</div></legend>
					<table class="normal_table">
						<tr>
							<td width="110" style="text-align:right;">Name:</td>
							<td><input name="itemName" type="text" value="${itemModel.itemName}" class="easyui-validatebox span2 normal_input" data-options="required:true"></td>
						</tr>
					</table>
				</fieldset>

				<fieldset style="border:0px">
					<legend><div class="pagetitle">数据库配置</div></legend>
					<table class="normal_table" border="0">
					<tr>
						<td width="110" style="text-align:right;">数据库类型:</td>
						<td>
							<select id="dbType" name="dbType" class="normal_input">
								<option value='1'>MySql</option>
								<option value='2'>SQL Server 2008</option>
							</select>
						</td>
					</tr>
					<tr>
						<td style="text-align:right;">数据库 URL:</td>
						<td><input id="url" name="url" type="text" size="22" value="${itemModel.url}" class="easyui-validatebox span2 normal_input" data-options="required:true"></td>
					</tr>
					<tr>
						<td style="text-align:right;">数据库用户名:</td>
						<td><input id="userName" name="userName" type="text" value="${itemModel.userName}" class="easyui-validatebox span2 normal_input" data-options="required:true"></td>
					</tr>
					<tr>
						<td style="text-align:right;">数据库密码:</td>
						<td><input id="password" name="password" type="text" value="${itemModel.password}" class="easyui-validatebox span2 normal_input"></td>
					</tr>
				</table>
				</fieldset>
				
				<table class="normal_table" width="100%" border="0">
					<tr>
						<td width="110" style="text-align:right;">
						</td>
						<td>
							<input type="button" name="button" id="button" value="完成" onClick="submitForm();"/>
							<input type="button" name="button" id="button1" value="测试" onClick="testConnection();"/>
						</td>
					</tr>
				</table>
				<script>
					$.extend($.fn.validatebox.defaults.rules, {
					   number_int:{
					      validator: function  (value, param) {
					         return  /^\d+(?:\d*)$/.test(value) && value > 0;
					      },
					      message: '必须大于0的数字'
					   }
					});
					
					function submitForm() {
						$('#form').form({
							url : '${pageContext.request.contextPath}/DBFactoryServer?op=saveFactoryItem',
							onSubmit : function() {
								var isValid = $(this).form('validate');
								return isValid;
							},
							success : function(result) {
								var tree = $.center_panel.factory_tree;
								tree.tree("reload");
							}
						}).submit();
			    	}

			    	function testConnection() {
			    		var href = "${pageContext.request.contextPath}/DBFactoryServer?op=testConnection";
			    		var dbType = $("#dbType").val();
			    		var url = $("#url").val();
			    		var userName = $("#userName").val();
			    		var password = $("#password").val();

			    		$.post(
							href,
							{dbType : dbType, url : url, userName : userName, password : password},
							function(result){
						   		alert(result);
							}
						);
			    	}
			  </script>
			</form>
		</div>
	</div>
  </body>
</html>